package com.hubu.game.entity.converter;

import com.hubu.game.entity.enumerate.OccuEnum;
import com.hubu.game.entity.pojo.Role;
import com.hubu.game.entity.vo.RoleVo;
import org.apache.ibatis.reflection.ArrayUtil;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Mapper
@Component
public interface RoleConverter {
    RoleConverter INSTANCE = Mappers.getMapper(RoleConverter.class);

    @Mappings({
            @Mapping(target = "currentOccu", qualifiedByName = "convertOccu"),
            @Mapping(target = "occuDetail", qualifiedByName = "convertOccuDetail"),
            @Mapping(target = "occuTimes", qualifiedByName = "convertOccuTimes"),
    })
    RoleVo role2RoleVo(Role role);
    Role roleVo2Role(RoleVo roleVo);

    List<RoleVo> roleList2RoleVoList(List<Role> roleList);
    List<Role> roleVoList2RoleList(List<RoleVo> roleVoList);

    /**
     * 当前职业：
     * 0:武士
     * 1:文士
     * 2:异人
     * @param occu
     * @return
     */
    @Named("convertOccu")
    default String convertOccu(String occu) {
        return OccuEnum.getDescriptionByCode(occu);
    }

    /**
     * 0000:表示武武武武
     * @param occuDetail
     * @return
     */
    @Named("convertOccuDetail")
    default String convertOccuDetail(String occuDetail) {
        //"0000"-->"武武武武"
        String[] splitDetail = occuDetail.split("");

        //转化为流对象，
        // 在将code映射为description,
        // 然后再转化为Array进行join
        return String.join("",Arrays.stream(splitDetail)
                .map(detailCode -> OccuEnum.getDescriptionByCode(detailCode).split("")[0])
                .toArray(String[]::new));
    }

    /**
     * 转职次数：
     *  3：三转
     *  2：二转
     *  1：一转
     *  0：未转
     * @param occuDetail
     * @return
     */
    @Named("convertOccuTimes")
    default String convertOccuTimes(Integer occuTimes) {
        //3-->三转
        String convertedOccuTimes="";
        switch (occuTimes){
            case 0:
                convertedOccuTimes="未转";
                break;
            case 1:
                convertedOccuTimes="一转";
                break;
            case 2:
                convertedOccuTimes="二转";
                break;
            case 3:
                convertedOccuTimes="三转";
                break;

        }


       return convertedOccuTimes;
    }

}
